package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.fieldCase.FieldCaseDTO;
import com.etone.smartAudit.service.FieldCaseService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.fieldCase.ContributorTopVO;
import com.etone.smartAudit.vo.fieldCase.FieldCaseDetailVO;
import com.etone.smartAudit.vo.fieldCase.FieldCaseListVO;
import com.etone.smartAudit.vo.fieldCase.FieldCaseSimpleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author: sovy
 * @date: 2020/8/19
 * @description:
 */
@AllArgsConstructor
@RestController
@Api(tags = "案例库管理", value = "FieldCaseController")
public class FieldCaseController {

    private FieldCaseService fieldCaseService;

    @ApiOperation(value = "案例库列表《完》")
    @GetMapping("/fieldCases")
    public PagerVO<FieldCaseListVO> list(
            @ApiParam(value = "领域id") @RequestParam(required = false) Integer fieldId,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return fieldCaseService.findList(fieldId, page, pageSize);
    }

    @ApiOperation(value = "案例库列表《完》")
    @GetMapping("/fieldCases/simple")
    public PagerVO<FieldCaseSimpleVO> listSimple(
            @ApiParam(value = "领域id") @RequestParam(required = false) Integer fieldId,
            @ApiParam(value = "排序方式,默认创建时间顺序,lookCnt:查看次数倒序") @RequestParam(required = false) String orderBy,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return fieldCaseService.listSimple(fieldId, orderBy, page, pageSize);
    }

    @ApiOperation(value = "案例库详情《完》")
    @GetMapping("/fieldCases/{id}")
    public FieldCaseDetailVO detail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "是否记录查看次数（默认false）", defaultValue = "false") @RequestParam Boolean isLook
    ) {
        return fieldCaseService.getFieldCaseDetailVO(id, isLook);
    }


    @ApiOperation(value = "案例库贡献人排名《完》")
    @GetMapping("/fieldCases/contributor/{top}")
    public List<ContributorTopVO> contributorTop(
            @ApiParam(value = "top", required = true) @PathVariable Integer top
    ) {
        return fieldCaseService.contributorTop(top);
    }

    @AdminOperation(module = "审计案例库", item = "添加")
    @ApiOperation(value = "增加案例《完》")
    @PostMapping("/fieldCases")
    public void add(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "案例表单详情") @RequestBody @Validated FieldCaseDTO fieldCaseDTO
    ) {
        fieldCaseService.save(fieldCaseDTO, null, admin);
    }

    @AdminOperation(module = "审计案例库", item = "编辑")
    @ApiOperation(value = "编辑案例《完》")
    @PutMapping("/fieldCases/{id}")
    public void edit(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "案例表单详情") @RequestBody @Validated FieldCaseDTO fieldCaseDTO
    ) {
        fieldCaseService.save(fieldCaseDTO, id, admin);
    }

    @AdminOperation(module = "审计案例库", item = "删除")
    @ApiOperation(value = "删除案例《完》")
    @DeleteMapping("/fieldCases/{id}")
    public void delete(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "领域名称(用于日志)") @RequestParam String fieldName,
            @ApiParam(value = "案例名称(用于日志)") @RequestParam String fieldCaseName
    ) {
        fieldCaseService.delete(id);
    }

    @AdminOperation(module = "审计案例库", item = "下载")
    @ApiOperation(value = "下载案例(仅仅用于统计下载次数)《完》")
    @GetMapping("/fieldCases/{id}/download")
    public void download(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "领域名称(用于日志)") @RequestParam String fieldName,
            @ApiParam(value = "案例名称(用于日志)") @RequestParam String fieldCaseName,
            @ApiParam(value = "文件名称(用于日志)") @RequestParam String fileName
    ) {
        fieldCaseService.download(id);
    }
}
